﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using LGE;

namespace MagicFight
{
    class Projectile : GameObject
    {
        Character caster;
        public GameObject target = null;

        public bool canCollideWithCaster = false;
        public bool canCollideWithProjectile = true;
        public float velocity = 10f;
        double lifeTime = 0;
        public int maxLifeTime = 5000;
        public bool collided = false;
        public bool ricochet = false;

        SpriteSheet spriteSheet = null;

        public Projectile(Character caster, Texture2D sprite)
            : base(sprite)
        {
            this.caster = caster;
            position = caster.position;
            direction = caster.direction;
        }

        public Projectile(Character caster, Texture2D sprite, int nFrames)
            : base(sprite)
        {
            this.caster = caster;
            position = caster.position;
            direction = caster.direction;
            spriteSheet = new SpriteSheet(this, sprite.Width/nFrames, sprite.Height, 100, true);
            HitBox = sourceRectangle = new Rectangle(0, 0, spriteSheet.frameWidth, spriteSheet.frameHeight);
            origin = new Vector2(sourceRectangle.Center.X, sourceRectangle.Center.Y);
        }

        public override void Update(GameTime gameTime)
        {
            if (!alive) return;
            if (collided) return;

            lifeTime += gameTime.ElapsedGameTime.TotalMilliseconds;
            if (lifeTime > maxLifeTime) { alive = false; return; }
            if (spriteSheet != null) spriteSheet.Update(gameTime);
            position += direction * velocity;

            CheckCollision();
        }

        private void CheckCollision()
        {
            foreach (GameObject obj in ps.gameObjects)
            {
                if (obj == null) continue;
                if (!obj.alive) continue;
                if (obj == this) continue;
                if (!canCollideWithCaster && obj == caster) continue;
                if (!canCollideWithProjectile && obj is Projectile) continue;
                if (Collision(obj))
                {
                    collided = true;
                    if (obj is Projectile) (obj as Projectile).collided = true;
                    target = obj;
                    break;
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (collided) return;
            base.Draw(spriteBatch);
        }

        public override void MapCollision(Vector2 dir)
        {
            if (!ricochet) { alive = false; return; }
            if (dir == Vector2.Zero) return;
            if (dir == Direction.Up || dir == Direction.Down) direction.Y *= -1;
            if (dir == Direction.Left || dir == Direction.Right) direction.X *= -1;
        }
    }
}
